package com.jindouyun.provider;

import com.jindouyun.provider.server.RpcServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.annotation.Resource;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @className: com.jindouyun.provider.ProviderApplication
 * @description: 生产者启动类
 * @author: tong.li
 * @createTime: 2021/10/12 17:43
 * @copyright: Tongcheng-Elong Holdings Limited
 */
@SpringBootApplication
public class ProviderApplication implements CommandLineRunner, DisposableBean {

    // 服務器  负载   熔断   监控  调用
    private final Logger logger = LoggerFactory.getLogger(ProviderApplication.class);

    @Resource
    private RpcServer rpcServer;

    private ExecutorService startTheardPool = null;

    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }

    /**
     * 启动时加载
     * @param args
     */
    @Override
    public void run(String... args) throws ExecutionException, InterruptedException {
        startTheardPool = Executors.newFixedThreadPool(1);
        String host = "127.0.0.1";
        int port = 9999;
        startTheardPool.submit(() -> {
            rpcServer.start(host,port);
        });

    }

    /**
     * Spring容器销毁时执行关闭項目中的所有资源
     */
    @Override
    public void destroy() {
        rpcServer.close();
        if (startTheardPool != null) {
            startTheardPool.shutdown();
        }
        logger.info("All thread pool has bean shutdowned");

    }
}
